package com.songyuquan.webcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songyuquan.webcs.mapper.CartMapper;
import com.songyuquan.webcs.mapper.ProductMapper;
import com.songyuquan.webcs.pojo.Cart;
import com.songyuquan.webcs.pojo.Product;
import com.songyuquan.webcs.service.CartService;
import com.songyuquan.webcs.vo.CartVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CartVO> findAllCartVOByUserId(Integer id) {
        List<CartVO> cartVOList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", id);
        List<Cart> list = cartMapper.selectList(queryWrapper);
        for (Cart cart : list) {
            CartVO cartVO = new CartVO();
            Product product = productMapper.selectById(cart.getProductId());
            BeanUtils.copyProperties(product, cartVO);
            BeanUtils.copyProperties(cart, cartVO);
            cartVO.setId(cart.getId());
            cartVOList.add(cartVO);
        }
        return cartVOList;
    }
}
